﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ccit.ClassLibrary;

namespace Ccit.Utils
{
    /// <summary>
    /// http 请求操作类
    /// </summary>
    public class RequestContext
    {
        /// <summary>
        ///		取得当前页面的文件名称[带路径]
        ///		例如：http://Tim:2173/MOA/SystemManage/Sys_User_list.aspx，返回：SystemManage/Sys_User_list.aspx
        /// </summary>
        /// <returns></returns>
        public static string GetPageFileNameWithPath()
        {
            string sPageFullPath = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();
            string sFileName = (sPageFullPath == "") ? "" : sPageFullPath.Substring(sPageFullPath.LastIndexOf("/") + 1);

            string sUrl = sPageFullPath.Substring(0, sPageFullPath.LastIndexOf("/")).ToLower();

            //取出Url后,需要去除虚拟目录名称			
            string virtualDir = GetVirsualPath().ToLower();
            int pos = sUrl.IndexOf(virtualDir);
            if (pos > -1)
            {
                sUrl = sUrl.Substring(pos + virtualDir.Length);
            }

            if (sUrl == virtualDir)
                sUrl = string.Empty;

            string path = string.Empty;
            if (sUrl == "")
            {
                path = sFileName;
            }
            else
            {
                if (sUrl.StartsWith("/"))
                {
                    path = sUrl.Substring(1) + "/" + sFileName;
                }
            }

            return path;
        }

        /// <summary>
        /// 取得当前页面的文件名称（不带路径），例如：http://Tim:2173/MOA/SystemManage/Sys_User_list.aspx，返回：Sys_User_list.aspx
        /// </summary>
        /// <returns></returns>
        public static string GetPageFileName()
        {
            string sFullFileName = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();
            string sFileName = (sFullFileName == "") ? "" : sFullFileName.Substring(sFullFileName.LastIndexOf("/") + 1);
            return sFileName;
        }

        /// <summary>
        /// 取得当前HTTP请求的虚拟目录名称，例如：http://Tim/MOA 返回：MOA
        /// </summary>
        /// <returns></returns>
        public static string GetVirsualPath()
        {
            string sAppPath = HttpContext.Current.Request.ServerVariables["APPL_MD_PATH"].ToString();
            string sTempAppPath = sAppPath;
            int dirLen = sTempAppPath.IndexOf("/") + 1;
            int dirCount = 0;
            while (dirLen > 0)
            {
                dirCount += 1;
                sTempAppPath = sTempAppPath.Substring(dirLen);
                dirLen = sTempAppPath.IndexOf("/") + 1;
            }
            string sVirsualPath = "";
            if (dirCount > 4)
                sVirsualPath = (sAppPath == "") ? "" : sAppPath.Substring(sAppPath.LastIndexOf("/") + 1);

            return sVirsualPath;
        }

        /// <summary>
        ///  取得当前HTTP请求的相对路径. 
        ///  如: 当前页面 http://Tim:2173/MOA/SystemManage/Sys_User_list.aspx,则返回../ 
        /// </summary>
        /// <returns></returns>
        public static string GetRelativelyPath()
        {
            string sPagePath = RequestContext.GetPageFileNameWithPath();	//获取本页面名称
            // 获取相对路径
            string sRelativePath = "";
            if (!string.IsNullOrEmpty(sPagePath))
            {
                StringLib strlInstance = new StringLib(sPagePath, '/');
                int separatorCount = strlInstance.ItemCount - 1;
                while (separatorCount > 0)
                {
                    sRelativePath += "../";
                    separatorCount--;
                }
                strlInstance = null;
            }

            return sRelativePath;
        }

        /// <summary>
        ///		从Request对象的QueryString或者Form集合中获取ParamName变量的值
        /// </summary>
        /// <param name="paramName">
        ///		变量名称
        /// </param>
        /// <param name="defaultValue">
        ///		如果不存在该变量,需要返回的默认值
        /// </param>
        /// <returns></returns>
        public static string GetParamValueByName(string paramName, string defaultValue)
        {
            string sReturn = (null != HttpContext.Current.Request.Params[paramName]) ?
                HttpContext.Current.Request.Params[paramName].ToString() : defaultValue;

            return sReturn;
        }
    }
}
